using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using BenevolentSunData;


namespace BenevolentSun
{
    public class BenevolentSun : Game
    {

        #region Components
        ScreenManager screenManager;
        #endregion


        #region Game Data
        public static LevelManifest LevelManifest;
        #endregion


        #region Graphics
        GraphicsDeviceManager graphics;

        private static DepthFormat SelectStencilMode()
        {
            // Check stencil formats
            GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter;
            SurfaceFormat format = adapter.CurrentDisplayMode.Format;
            if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth24Stencil8))
                return DepthFormat.Depth24Stencil8;
            else if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth24Stencil8Single))
                return DepthFormat.Depth24Stencil8Single;
            else if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth24Stencil4))
                return DepthFormat.Depth24Stencil4;
            else if (adapter.CheckDepthStencilMatch(DeviceType.Hardware, format,
                format, DepthFormat.Depth15Stencil1))
                return DepthFormat.Depth15Stencil1;
            else
                throw new InvalidOperationException(
                    "Could Not Find Stencil Buffer for Default Adapter");
        }
        #endregion


        #region Initialization
        public BenevolentSun()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 1280;           // xbox res
            graphics.PreferredBackBufferHeight = 720;
            graphics.SynchronizeWithVerticalRetrace = false;
            graphics.PreferredDepthStencilFormat = SelectStencilMode();

            IsFixedTimeStep = false;

            Content.RootDirectory = "Content";
        }

        
        protected override void Initialize()
        {
            AudioManager.Initialize(this, @"Content\Audio\BenevolentSunAudio.xgs", @"Content\Audio\Wave Bank.xwb", @"Content\Audio\Sound Bank.xsb");

            InputManager.Initialize(this);

            screenManager = new ScreenManager(this);
            Components.Add(screenManager);

            Components.Add(new FrameRateCounter(this));

            base.Initialize();
        }

        
        protected override void LoadContent()
        {
            LevelManifest = Content.Load<LevelManifest>("LevelManifest");

            screenManager.AddScreen(new SplashScreen());

            base.LoadContent();
        }
        #endregion


        #region Update
        protected override void Update(GameTime gameTime)
        {
            InputManager.Update(gameTime);

            base.Update(gameTime);
        }
        #endregion


        #region Drawing
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            base.Draw(gameTime);
        }
        #endregion

    }

}
